package com.sphere.freereader;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.sphere.freereader.impl.ReaderImageCache;

/**
 * Created by aquarius on 2016/9/3.
 */
public abstract class BaseFragment extends Fragment {


    protected static final int MSG_HANDLE_OTHER = -100;
    protected static final int MSG_SAVE_CURRENT_BOOKLIST = 1 << 2;

    protected Context mContext;
    protected RequestQueue mRequestQueue;   // 请求队列
    protected ImageLoader mImageLoader;
    protected int mCategoryType;

    protected Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_HANDLE_OTHER:
                    handleOtherMessage(msg);
                break;
                default:
                    break;
            }
            handleThreadMessage(msg);
            super.handleMessage(msg);
        }
    };


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRequestQueue = MyApplication.getRequestQueue();
        ReaderImageCache imageCache = ReaderImageCache.getInstance();
        mImageLoader = new ImageLoader(mRequestQueue, imageCache);
        getDataFromArguments(getArguments());
        mCategoryType = setCategoryType();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        return inflaterView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initialViews();
        initialDatas();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected abstract View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
    protected abstract void initialViews();
    protected abstract void initialDatas();
    protected abstract void getDataFromArguments(Bundle data);
    protected abstract void handleThreadMessage(Message msg);
    protected abstract void handleOtherMessage(Message msg);
    protected abstract int setCategoryType();


}
